package action

import scala.swing._
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.Transferable
import javax.swing.JOptionPane
import lang.LOC
import scala.io._
import parser._
import gui._

/**
 * Diese Klasse erbt von der Action-Klasse und ist fuer die Funktionen des Bearbeiten-Menues
 * zustaendig. Hierzu wird ueber das Attribut subAction die jeweilige Aktion aufgerufen.
 */

case class MenuEdit(name: String, tt: Layout, subAction: Int) extends Action(name) {

  def apply() = {
    subAction match {
      case 1 => //Ausschneiden
        var tp = tt.getFocusedTextArea
        if(tp != null) {
          var sel = new StringSelection(tp.peer.getSelectedText())
          var sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
          sysClip.setContents(sel, null)
          tp.peer.replaceSelection("")
        }

      case 2 => //Kopieren
        var tp = tt.getFocusedTextArea
        if(tp != null) {
          var sel = new StringSelection(tp.peer.getSelectedText())
          var sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
          sysClip.setContents(sel, null)
        }

      case 3 => //Einfuegen
        var tp = tt.getFocusedTextArea
        if(tp != null) {
          var sel = new StringSelection(tp.peer.getSelectedText())
          var sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
          var ins: Transferable = sysClip.getContents(null)
          if(ins != null) {
            var inst: String = ins.getTransferData(DataFlavor.stringFlavor).
                                                                      asInstanceOf[String]
            tp.peer.replaceSelection(inst)
          }
        } 
        
      case 4 => //Alles auswaehlen
        var tp = tt.getFocusedTextArea
        if(tp != null) {
          tp.peer.selectAll
        }

      case 6 => //Suchen
        var suchstr = JOptionPane.showInputDialog(tt.peer, LOC.get("SucheText") + ":",
                                                  LOC.get("Suche"),
                                                  JOptionPane.QUESTION_MESSAGE, icon, null,
                                                  null) match {
          case x:String => x
          case _ => null
        }
        if(suchstr != null) {
          var tp = tt.getFocusedTextArea
          if(tp != null) {
            //Suche
            /* var sel = new StringSelection(tp.peer.getSelectedText())
            var sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
            var ins: Transferable = sysClip.getContents(null)
            if(ins != null) {
              var inst: String = ins.getTransferData(DataFlavor.stringFlavor).
                                                                           asInstanceOf[String]
              tp.peer.replaceSelection(inst)
            } */
          } 
        }
        
        //println("SUCHEN")

      case 8 =>

      case 9 =>

      case 10 =>

      case 12 =>

      case _ =>
        println("Action2.scala")
    }
  }
  
}

        
 